package edu.uc.labs.MobilePrinting.PrintUtils;

import edu.uc.labs.MobilePrinting.Configurator;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.Logger;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;

/**
 * Writes or Gets a cache file based on printer settings
 * @param printer
 */
public class PrinterCache {

    
    private static Logger logger = Configurator.LOGGER;
    private static String cache_dir = Configurator.CACHEDIR;

    
    /*
     * The name of the file to write is the name of the printer zone
     * There will be one cache file per printer zone so when the
     * user enters a new zone that printer will be the default
     * printer installed, which is also the last printer in that
     * zone that the user installed
     */
    public static void writeCache(Printer p) throws ParserConfigurationException, IOException{
        DocumentBuilder builder;
        Document doc;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        builder = factory.newDocumentBuilder();

        doc = builder.newDocument();
        Element root = doc.createElement("CONFIGURATIONS");
        Element e = doc.createElement("PRINTERS");
        Element e2 = doc.createElement("PRINTER");

        Text name = doc.createTextNode("" + p.getName());
        Element e3 = doc.createElement("NAME");
        e3.appendChild(name);

        Text server = doc.createTextNode("" + p.getServer());
        Element e4 = doc.createElement("SERVER");
        e4.appendChild(server);

        Text queue = doc.createTextNode("" + p.getQueue());
        Element e5 = doc.createElement("QUEUE");
        e5.appendChild(queue);

        Text driver = doc.createTextNode("" + p.getPPD());
        Element e6 = doc.createElement("DRIVER");
        e6.appendChild(driver);

        Text zone = doc.createTextNode("" + p.getZone());
        Element e7 = doc.createElement("ZONE");
        e7.appendChild(zone);

        Text location = doc.createTextNode("" + p.getLocation());
        Element e8 = doc.createElement("LOCATION");
        e8.appendChild(location);

        e2.appendChild(e3);
        e2.appendChild(e4);
        e2.appendChild(e5);
        e2.appendChild(e6);
        e2.appendChild(e7);
        e2.appendChild(e8);

        e.appendChild(e2);
        root.appendChild(e);
        doc.appendChild(root);

        DOMImplementation impl = doc.getImplementation();
        DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
        LSSerializer ser = implLS.createLSSerializer();
        String out = ser.writeToString(doc);
        logger.debug(out);

         File f = new File(cache_dir  + Configurator.CACHE +
                           Configurator.DELIM + p.getZone() + ".xml");
         FileWriter fstream = new FileWriter(f);
         BufferedWriter bOut = new BufferedWriter(fstream);
         bOut.write(out);
         bOut.close();
    }

    public static void getCache(String zone){
        

    }





}
